#!/usr/bin/python
# -*- coding: utf-8 -*-
# script name : facialSysWindow
# author: lizhi
# date:  2013/3/28

import maya.cmds as mc
import sys
import maya.cmds as mel
sys.path.append('E:/script/facial_sys')
sys.path.append('E:/script/LZ_Tools')

#---------------------------------------------------------------------------
import createJoints02 
import commonScripts02
reload(commonScripts02)
import connectBridgeAttr
reload(connectBridgeAttr)

class facialSysWindow(createJoints02.buildBaseSetting,connectBridgeAttr.connectSysAttr):
    def facialSysWindow(self):
        facialSysWindow = 'facialSysWindow'
        if mc.window(facialSysWindow,ex = True):
            mc.deleteUI(facialSysWindow)
        
        mc.window(facialSysWindow,widthHeight=(350, 400),t='facial RigSys Window_v1.01',menuBar = True,rtf=True,s=False)
        mc.rowColumnLayout('RigSysMain',numberOfColumns=1,w=300,cal = (5,'center'),m=True)
        
        mc.button('referencTemplePlane',l='Load reference Temple',w=300,h=30,c=lambda *args:self.WDreferenceScence())
        
        mc.rowColumnLayout('mirrorlocatorLout',numberOfColumns=2,w=300,cal = (5,'center'),m=True)

        mc.radioButtonGrp('WDmirrorLoc',numberOfRadioButtons=2,w=170,labelArray2=['L To R', 'R To L'],sl=1)
        mc.button('mirror Locator',l='mirror Locator',w=178,h=30,c=lambda *args:self.WDmirrorLocator())
        
        mc.setParent('..')
        mc.separator( w=350, style='in' )
        
        mc.button('buildSettings',l='build Settings',w=300,h=30,c=lambda *args:self.WDbuildFrame())
        
        mc.separator( w=350, style='in' )
        mc.rowColumnLayout('SDKbridgemenuLout',numberOfColumns=2,w=300,cal = (5,'center'),m=True)
        mc.text(l='       SDK_bridge_obj :     ')
        mc.optionMenu('SDKbridgemenu',label='',w=200,h=30,changeCommand=lambda *args:self.listBridgeAttr())#, changeCommand=printNewMenuItem
        

        mc.menuItem( label='facial_mouth_bridge' )
        mc.menuItem( label='facials_eyes_bridge' )
        mc.menuItem( label='facials_nose_cheek_bridge' )
        mc.menuItem( label='faciels_bridge' )
        mc.menuItem( label='PS_Normal_bridge' )
        mc.menuItem( label='PS_Smile_bridge' )
        mc.menuItem( label='PS_Frown_bridge' )
        mc.setParent('..')
        mc.rowColumnLayout('SDKbridgemudeLout',numberOfColumns=3,w=300,cal = (5,'center'),m=True)
        mc.checkBox('originalVis',label='original',v=True,w=100,cc=lambda *args:self.listBridgeAttr())
        mc.checkBox('leftVis',label='left',v=True,w=100,cc=lambda *args:self.listBridgeAttr())
        mc.checkBox('rightVis',label='right',v=False,w=100,cc=lambda *args:self.listBridgeAttr())

        #mc.setParent('..')
        mc.setParent('..')
        mc.paneLayout('setDrivenKeyLO',configuration='vertical2',w=350,h=250)
        mc.textScrollList('bridgeAttrlist',append=['open','side','forward','rotation','up_down','L_wide','R_wide','L_up_down','R_up_down','Mouth_LR','Mouth_UD','Mouth_FB','Mouth_Rot_X','Mouth_Rot_Y','Mouth_Rot_Z','C_up_lip_roll','C_low_lip_roll','L_lip_corner_follow','R_lip_corner_follow','L_up_lip_center_follow','R_up_lip_center_follow','L_low_lip_center_follow','R_low_lip_center_follow','C_up_lip_follow','C_low_lip_follow','up_lip_follow_angle'],w=150,selectCommand=lambda *args:self.listSDKerobj())
        
        mc.popupMenu(button=3,markingMenu=True)
        mc.menuItem('max' , label='finish',rp='N' ,c=lambda *args:self.goToVal('max') ) #label='N '
        mc.menuItem('def' , label='fix',rp='W' ,c=lambda *args:self.clearkey()) #label='S '
        mc.menuItem('min' , label='Minimum',rp='S' ,c=lambda *args:self.goToVal('min')) #label='S '
        mc.setParent(menu=True)  
        
        mc.textScrollList('SDKenAttrlist',numberOfRows=8, allowMultiSelection=True,showIndexedItem=4 ,w=150,m=True,selectCommand=lambda *args:self.WDselectSDKenobj())
        mc.popupMenu(button=3,markingMenu=True)
        mc.menuItem('max' , label='Maximum',rp='N' ,c=lambda *args:self.goToVal('max') ) #label='N '
        mc.menuItem('def' , label='default',rp='W' ,c=lambda *args:self.clearkey()) #label='S '
        mc.menuItem('min' , label='Minimum',rp='S' ,c=lambda *args:self.goToVal('min')) #label='S '
        mc.setParent(menu=True)        
        
        mc.setParent('..')
        mc.rowColumnLayout('SDKLO',numberOfColumns=4,w=300,cal = (5,'center'),m=True)
        mc.text(l='   ')
        mc.button(l='add To sdk',w=150,h=30,c=lambda *args:self.addToSDKlist())
        mc.text(l='      ')
        mc.button(l='remove sdk from list',w=150,c=lambda *args:self.removenSDK())
        
        mc.setParent('..')
        mc.separator(w=20,style='in' )

        mc.rowColumnLayout('SDKLO02',numberOfColumns=4,w=300,cal = (5,'center'),m=True)
        
        mc.text(l='        ')
        mc.checkBox('reSDKmod',l='mirror SDK',v=1,w=150,h=30)
        mc.text(l=' ')
        mc.button(l='modify SetDrivenDey',w=150,h=30)
        
        mc.showWindow()
        
        
    def WDreferenceScence(self):
        fileName=mc.fileDialog2(fm=1,okc= 'Reference' ,cap='Reference',ff='mayaAscii (*.ma *.MA);;mayaBinary (*.mb *.MB);;mel (*.mel *.MEL);;OBJ (*.obj *.OBJ);;audio (*.wav *.WAV *.aif *.AIF *.aiff *.AIFF *.aifc *.AIFC *.mp3 *.MP3 *.snd *.au *.AU *.SND *.sd2 *.SD2 *.mov *.MOV *.qt *.QT);;move (*.mov *.MOV);;EPS (*.eps);;Adobe(R) Illustrator(R) (*.ai);;image (*.als *.ALS *.jpeg *.JPEG *.jpg *.JPG *.pntg *.PNTG *.ps *.PS *.png *.PNG *.psd *.PSD *.pict *.PICT *.qt *.QT *.qtif *.QTIF *.sgi *.SGI *.tga *.TGA *.tif *.TIF *.bmp *.BMP *.tiff *.TIFF *.iff *.IFF *.rgb *.RGB *.tdi *.TDI *.gif *.GIF *.exr *.EXR);;fluidCache (*.mcfi *.mcfp );;editMA (*.editMA);;editMB (*.editMB);;ASS (*.ass);;studioImport (*.wire);;JT_DC (*.jt);;PTC_DC (*.prt.* *.asm.* *.g);;SPF_DC (*.wire);;STEP_DC (*.stp *.step);;STL_DC (*.stl *.stla *.stlb);;SW_DC (*.sldprt *.sldasm);;IV_DC (*.iv);;CSB_DC (*.csb);;IGES_DC (*.iges *.igs);;DWG_DC (*.dwg);;DXF_DC (*.dxf);;CATIAV4_DC (*.model *.mdl *.session *.exp *.dlv *.dlv3 *.dlv4);;CATIAV5_DC (*.catproduct *.catpart *.cgr);;IPT_DC (*.ipt *.iam);;Maya Scene (*.mb *.ma *.MB *.MA);;video (*.avi *.AVI);;plug-in (*.mll *.so *.py *.pyc *.MLL *.SO *.PY *.PYC *.bundle *.BUNDLE);;text (*.txt *.TXT);;Best Guess (*.*);;ag (*.ag *.AG);;iff (*.iff *.IFF);;From Tool Settings (*.*);;Maya IFF (*.iff);;QuickTime Movie (*.qt);;MacPaint (*.pntg);;Photoshop (*.psd);;PNG (*.png);;JPEG (*.jpg);;QuickDraw (*.pict);;QuickTime Image (*.qtif);;SGI (*.sgi);;Targa (*.tga);;Tiff (*.tif);;Windows Bitmap (*.bmp);;EXR (*.EXR *.exr)')[0]
        

        
        self.referenceScence(fileName)

    
    def WDremoveReference(self):
        try:
            mc.referenceQuery('facial_prelocatorRN',isLoaded=True)
            mc.referenceEdit( 'facial_prelocatorRN', removeEdits=True )
            mc.referenceEdit( 'facial_prelocator.ma', removeEdits=True )

        except:
            sys.stdout.write('three is no reference named facial_prelocatorRN, please check it ')
    def WDmirrorLocator(self):
        modMirror=mc.radioButtonGrp('WDmirrorLoc',q=True,sl=True)
        self.mirrorLocator(modMirror)
        
    def WDbuildFrame(self):
        grpAllName=[]
        facial_Grp='facial_grp'
        if mc.objExists(facial_Grp)== False:
            BridgeGrp=self.OrganizeHierarchyRelations()
            #
            grpAllName.append(BridgeGrp)
            
            createJoints=self.createJoints()
            
            facialCtrlGrp=self.parentRelations()
            #
            grpAllName.append(facialCtrlGrp)
            
            addSDKGrp=self.addSDKGrp()
            offsetRootGrp=self.offsetRootGrp()
            facialOtherGrp=self.facial_jntOther()
            #
            grpAllName.append(facialOtherGrp[0])
            grpAllName.append(facialOtherGrp[1])
            
            createEyeCtrl=self.createEyeCtrl()

            facial_Grp=self.createfacialgrp()
            mc.parent(grpAllName,facial_Grp)
            
            addCtrlToSettings=self.addCtrlToSettings()
            self.eyeModifyRelations()
            self.connectBridgeAttr()
            self.setDKeyValue()
            
            self.followMouthSettings()
        else:
            sys.stdout.write('settings has existed , only one setting exists at one scene')
        
    def WDfuck(self):
        addCtrlToSettings=self.addCtrlToSettings()
    
    def listBridgeAttr(self):
        leftAttr=[]
        rightAttr=[] 
        originalAttr=[]
        
        driverobj=mc.optionMenu('SDKbridgemenu',q=True,v=True)
        attrChannelBox=mc.listAttr(str(driverobj),u=True,o=True,k=True)
        
        for i in attrChannelBox:
            if 'left' in i or 'L_' in i:
                leftAttr.append(i)

            elif 'right' in i or 'R_' in i:
                rightAttr.append(i)
                
        for k in attrChannelBox:
            if k in leftAttr or k in rightAttr:
                pass
            else:
                originalAttr.append(k)    

        orgAttrVis=mc.checkBox('originalVis',q=True,v=True)
        leftAttrVis=mc.checkBox('leftVis',q=True,v=True)
        rightAttrVis=mc.checkBox('rightVis',q=True,v=True)

        mc.textScrollList('bridgeAttrlist',ra=True,e=True)
        if orgAttrVis == True:
            mc.textScrollList('bridgeAttrlist',append=originalAttr,e=True)
        if leftAttrVis == True:
            mc.textScrollList('bridgeAttrlist',append=leftAttr,e=True)
        if rightAttrVis == True:
            mc.textScrollList('bridgeAttrlist',append=rightAttr,e=True)

        
    def listSDKerobj(self):
        driverobj=mc.optionMenu('SDKbridgemenu',q=True,v=True)

        bridgeAttr=mc.textScrollList('bridgeAttrlist',q=True,si=True)[0]

        driverAttr=str(driverobj)+'.'+bridgeAttr

        self.listSDKerAttr(driverAttr)
        mc.select(driverobj)
    def WDselectSDKenobj(self):
        
        SDKenObj=mc.textScrollList('SDKenAttrlist',q=True,si=True)
        mc.select(SDKenObj)
        
    def addToSDKlist(self):
        print(1)
        selobj=mc.ls(sl=True)
        
        driverobj=mc.optionMenu('SDKbridgemenu',q=True,v=True)
        
        bridgeAttr=mc.textScrollList('bridgeAttrlist',q=True,si=True)[0]

        driverAttr=str(driverobj)+'.'+bridgeAttr
        print(driverAttr)

        if len(selobj)==0:
            sys.stdout.write('please select one driven object at last  ! ')
        else:
            for i in selobj:
                print(i)
                self.setDrivenKey(driverAttr,i,0,[0,0,0,0,0,0])
                self.setDrivenKey(driverAttr,i,10,[0,0,0,0,0,0])
                self.setDrivenKey(driverAttr,i,-10,[0,0,0,0,0,0])
        self.listSDKerobj()
    def removenSDK(self):
        driverobj=mc.optionMenu('SDKbridgemenu',q=True,v=True)
        
        bridgeAttr=mc.textScrollList('bridgeAttrlist',q=True,si=True)[0]
        
        drivens=mc.textScrollList('SDKenAttrlist',q=True,si=True)
        
        driverAttr=str(driverobj)+'.'+bridgeAttr
        for i in drivens:
            self.RemoveSDKenAttr(driverAttr,i)
            self.listSDKerobj()
                #self.setDrivenKey(sideDriverAt,jaw,0,[0,0,0,0,0,0])
            